package concurrency;

import java.util.Map;

abstract class MapTest extends Tester<Map<Integer,Integer>> {
    MapTest(String testId, int readers, int writers) {
        super(testId, readers, writers);
    }

    @Override
    void startReadersAndWriters() {
        for (int i = 0; i < mReaders; i++) {
            sExec.execute(new Reader());
        }

        for (int i = 0; i < mWriters; i++) {
            sExec.execute(new Writer());
        }
    }

    class Reader extends TestTask {
        private long mResult = 0;

        @Override
        void test() {
            for (long i = 0; i < sTestCycles; i++) {
                for (int index = 0; index < sContainerSize; index++) {
                    mResult += mTestContainer.get(index);
                }
            }
        }

        @Override
        void putResults() {
            mReadResult += mResult;
            mReadTime += mDuration;
        }
    }

    class Writer extends TestTask {
        @Override
        void test() {
            for (long i = 0; i < sTestCycles; i++) {
                for (int index = 0; index < sContainerSize; index++) {
                    mTestContainer.put(index, mWriteDatas[index]);
                }
            }
        }

        @Override
        void putResults() {
            mWriteTime += mDuration;
        }
    }
}
